package com.omg.api.handle;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.omg.api.argument.Argument;
import com.omg.api.argument.CommentArgument;
import com.omg.api.result.Result;
import com.omg.api.result.body.CommentBody;
import com.omg.api.result.header.CommentHeader;
import com.omg.constant.Constants;
import com.omg.constant.LPUtility;
import com.omg.constant.ResponseCode;
import com.omg.persistence.dao.lifepoint.entity.LPComment;
import com.omg.persistence.dao.lifepoint.entity.LPUser;
import com.omg.persistence.delegate.lifepoint.LPCommentDelegate;
import com.omg.persistence.delegate.lifepoint.LPUserDelegate;

/**
 * 
 * @author xiazhiquan
 * 
 *         May 29, 2011
 */
public class CommentHandler extends Handler {

	private static final Logger LOG = Logger.getLogger(CommentHandler.class
			.getName());

	private static CommentHandler instance = null;

	static {
		instance = new CommentHandler();
	}

	public static CommentHandler getInstance() {
		return instance;
	}

	public String handle(Argument argument) throws Exception {

		String userid = ((CommentArgument) argument).getUserid();
		String content = ((CommentArgument) argument).getContent();
		String action = ((CommentArgument) argument).getAction();
		String activityid = ((CommentArgument) argument).getActivityid();
		String commentid = ((CommentArgument) argument).getCommentid();

		LPComment comment = null;

		if (action.equals(Constants.ACTION_POST)) {
			comment = new LPComment();
			comment.setCommentid(LPUtility.getCommentId());
			comment.setActivityid(activityid);
			comment.setContent(content);
			comment.setCreattime(System.currentTimeMillis());
			comment.setCreatorid(userid);
			comment.setUptime(System.currentTimeMillis());

			int ret = LPCommentDelegate.getInstance().addComment(comment);

			if (ret != 0) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_PostCommentFailed);
			}

		} else if (action.equals(Constants.ACTION_DELETE)) {
			comment = LPCommentDelegate.getInstance().getCommentByCommentId(
					commentid);

			if (comment == null) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_InvalidCommentId);
			}

			int ret = LPCommentDelegate.getInstance().deleteComment(commentid);

			if (ret != 0) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_DeleteCommentFailed);
			}

		} else if (action.equals(Constants.ACTION_EDIT)) {
			comment = LPCommentDelegate.getInstance().getCommentByCommentId(
					commentid);

			if (comment == null) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_InvalidCommentId);
			}

			comment.setCommentid(commentid);
			comment.setContent(content);
			comment.setUptime(System.currentTimeMillis());

			int ret = LPCommentDelegate.getInstance().updateComment(comment);

			if (ret != 0) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_UpdateCommentFailed);
			}
		} else if (action.equals(Constants.ACTION_GET)) {
			List<LPComment> comments = LPCommentDelegate.getInstance()
					.getCommentsByActivityId(activityid);
			for (LPComment c : comments) {
				LPUser creator_info = LPUserDelegate.getInstance()
						.getLPUserByUserid(userid);
				creator_info.setPasswd(null);
				creator_info.setSid(null);
				creator_info.setVersion(null);
				c.setCreatorid(null);
				c.setCreator_info(creator_info);
			}
			return getJson(comments);
		}

		LPUser creator_info = LPUserDelegate.getInstance().getLPUserByUserid(
				userid);
		creator_info.setPasswd(null);
		creator_info.setSid(null);
		creator_info.setVersion(null);
		comment.setCreatorid(null);
		comment.setCreator_info(creator_info);

		List<LPComment> comments = new ArrayList<LPComment>();
		comments.add(comment);
		return getJson(comments);
	}

	private final String getJson(List<LPComment> comments) throws Exception {
		try {

			// construct result
			Result result = new Result();

			// header
			CommentHeader header = new CommentHeader();
			header.setRespcode(ResponseCode.AP_E_Succ);
			result.setHeader(header);

			// body
			CommentBody body = new CommentBody();
			body.setComments(comments);
			result.setBody(body);

			return new Gson().toJson(result);
		} catch (Exception e) {
			e.printStackTrace();
			String errorMsg = e.getMessage();
			LOG.error(errorMsg, e);
			throw new Exception(""
					+ (errorMsg != null && !errorMsg.isEmpty() ? errorMsg
							: ResponseCode.AP_E_Info_UnkownError));
		}
	}

}
